<?php
class Shepherd_ActiveController extends Sss_Controller_Default
{
	public function activeAction()
	{
		$instanceTable = new Application_Model_Mapper_Instance();
		//get active code
		$params = $this->_request->getParams();
		$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);

		if($handle != Sss_Ultility_CommonViewParamater::UNDERFINE) {
			//get instance for check when not found
			$instance = $instanceTable->getInstanceByHandle($handle);
			if($instance) {
				$defaultNamespace = new Zend_Session_Namespace(Sss_Ultility_SessionName::SHEPHERD_SYSTEM . $this->getCurrentHandle());
				$defaultNamespace->dbShepherd = $instance->instanceid;
				$accountTable     = new Shepherd_Model_Mapper_Account();
				if(isset($params['activatecode'])){
					try
					{
						$activateCode = $params['activatecode'];
						$account = $accountTable->getUserByActiveCode($activateCode, $instance->instanceid);
						if($account) {
							if($account->isactivated == 1) {
								$this->view->assign(Sss_Ultility_CommonViewParamater::MSG_ACTIVE, sprintf(Resources::_('ACCOUNT_ACTIVEED'), "/$handle/user/login"));
								return;
							} else {
								$accountUpdate = $accountTable->findById($account->accountid,$instance->instanceid);
								$accountUpdate->setIsActivated(1);
								$passWordDefault   = Sss_Ultility_Common::genRandomString();
								$encryptedPassword = Sss_Crypt::factory()->encrypt($passWordDefault);
								//Update user of OTRS
								$otrs = new Sss_Ultility_OtrsServices();

								$userName = $account->username;

								if(isset($params['forgotpassword'])){
									$accountUpdate->setPassword($encryptedPassword);
									$accountId = $accountTable->save($accountUpdate,$instance->instanceid);
									//Update user in OTRS service when update user in ovis site
									$otrs->updateCustomer($userName.$account->accountid.$handle, $encryptedPassword , USER_SERVICE, PASSWORD_SERVICE);

									$subject = Resources::_('SUBJECT_FORGOT_PASSWORD');
									$bodyTitle = Resources::_('BODY_FORGOT_PASSWORD'). " \n \n"
									.Resources::_('NEW_PASSWORD') . ': '.$passWordDefault. " \n \n"
									.Resources::_('BODY5'). " \n"
									.Resources::_('BODY6');;
								} elseif (isset($params['resetpassword'])) {
									$accountUpdate->setPassword($encryptedPassword);
									$accountId = $accountTable->save($accountUpdate, $instance->instanceid);
									//Update user in OTRS service when update user in ovis site
									$otrs->updateCustomer($userName.$account->accountid.$handle, $encryptedPassword , USER_SERVICE, PASSWORD_SERVICE);
									$subject = Resources::_('SUBJECT_RESET');
									$bodyTitle    = Resources::_('DEAR').' '.$account->fullname.',' . " \n \n"
									.Resources::_('BODY1'). " \n \n"
									.Resources::_('BODY3').' '.$passWordDefault. " \n \n"
									.Resources::_('BODY4'). " \n \n"
									.Resources::_('BODY5'). " \n"
									.Resources::_('BODY6');
								} else {
									$accountId = $accountTable->save($accountUpdate, $instance->instanceid);
									$this->view->assign(Sss_Ultility_CommonViewParamater::MSG_ACTIVE, sprintf(Resources::_('ACCOUNT_ACTIVE_SUCCESSFULL'), "/$handle/user/login"));
									return;
								}
								$body    = $bodyTitle;
								// update user shepherd BI
								Sss_Ultility_Common::updateUserBi($instance->instanceid.$userName,$encryptedPassword,$userName);
								//End update user shepherd BI
								Sss_Ultility_Common::sendMail(EMAIL_SYSTEM, $account->email, $subject, $body);
								$this->view->assign(Sss_Ultility_CommonViewParamater::MSG_ACTIVE, sprintf(Resources::_('ACCOUNT_ACTIVE_SUCCESSFULL'), "/$handle/user/login"));
								return;
							}
						} else {
							$this->view->assign(Sss_Ultility_CommonViewParamater::MSG_ACTIVE, Resources::_('ACCOUNT_ACTIVE_FAIL'));
							return;
						}
					} catch(exception $e) {
						Sss_Ultility_Common::logger()->log($e, Zend_Log::ERR);
						$this->view->assign(Sss_Ultility_CommonViewParamater::MSG_ACTIVE, Resources::_('ACCOUNT_ACTIVE_FAIL'));
						return;
					}
				}
			}
		}
	}
}